<?php
/*
 * Created on 2010-12-29
 * by lixilei
 */

class main extends spController{
	
	/**
	 * 转到首页/登录页
	 */
	public function index(){
		if(!empty($_SESSION["lexiang_login"]) && $_SESSION["lexiang_login"] == _SUCCESS){
			$this->results = $_SESSION;
			// 记录并调度信息
			$toURL = "home.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "main&index", $_SESSION["userInfo"]["user_id"]);
			$this->display($toURL);
		} else{
			// 记录并调度信息
			$toURL = "index.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "main&index");
			$this->display($toURL);
		}
	}
	
	/**
	 * 新用户注册
	 */
	public function register(){
		// 获取参数
		$email = trim($_POST["email"]);
		$password = trim($_POST["password"]);
		$password2 = trim($_POST["password2"]);
		
		if($email == NULL || $password == NULL || $password2 == NULL){
			$this->results = array("ret"=>_FAIL, "errMsg"=>_EMAIL_OR_PWD_NULL);
			if($email != NULL){
				$this->results += array("email"=>$email);
			}
			// 记录并调度信息
			$toURL = "register.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "main&register&error&argsNull");
			$this->display($toURL);
			return;
		}
		if(!eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}",$email)){
			$this->results = array("ret"=>_FAIL, "errMsg"=>_EMAIL_ERROR);
			// 记录并调度信息
			$toURL = "register.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "main&register&error&emailError");
			$this->display($toURL);
			return;
		}
		if($password != $password2){
			$this->results = array("ret"=>_FAIL, "errMsg"=>_PASSWORD_VERIFY_ERROR);
			$this->results += array("email"=>$email);
			// 记录并调度信息
			$toURL = "register.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "main&register&error&passwordError");
			$this->display($toURL);
			return;
		}
		
		// 执行业务
		$userBiz = spClass("UserBiz", null, BIZ_PATH."UserBiz.php");
		$result = $userBiz->register($email, $password);
		dump($result);
		// 结果处理
		if($result == FALSE){
			$this->results = array("ret"=>_FAIL, "errMsg"=>_EMAIL_EXIST);
			// 记录并调度信息
			$toURL = "register.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "main&register&error&emailExist");
			$this->display($toURL);
		} else{
			session_start();
			unset($result["password"]);
			$_SESSION["userInfo"] = $result;
			$_SESSION["lexiang_login"] = _SUCCESS;
			$this->results = $_SESSION;	
			// 记录并调度信息
			$toURL = "home.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "main&register", $_SESSION["userInfo"]["user_id"]);
			$this->display($toURL);
		}
	}
	
	/**
	 * 用户登录
	 */
	public function login(){
		// 获取参数	
		$email = trim($_POST["email"]);
		$password = trim($_POST["password"]);
		
		if($email == NULL || $password == NULL){
			$this->results = array("ret"=>_FAIL, "errMsg"=>_EMAIL_OR_PWD_NULL);
			if($email != NULL){
				$this->results += array("email"=>$email);
			}
			// 记录并调度信息
			$toURL = "index.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "main&login&error&argsNull");
			$this->display($toURL);
			return;
		}
		// email格式错误
		if(!eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}",$email)){
			$this->results = array("ret"=>_FAIL, "errMsg"=>_EMAIL_ERROR);
			// 记录并调度信息
			$toURL = "index.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "main&login&error&emailError");
			$this->display($toURL);
			return;
		}
		
		// 执行业务
		$userBiz = spClass("UserBiz", null, BIZ_PATH."UserBiz.php");
		$result = $userBiz->login($email, $password);
		
		// 结果处理
		if($result == FALSE){
			$this->results = array("ret"=>_FAIL, "errMsg"=>_LOGIN_ERROR);
			$this->results += array("email"=>$email);
			// 记录并调度信息
			$toURL = "index.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "main&login&error&argsError");
			$this->display($toURL);
		} else{
			session_start();
			unset($result["password"]);
			$_SESSION["userInfo"] = $result;
			$_SESSION["lexiang_login"] = _SUCCESS;
			$this->results = $_SESSION;
			// 记录并调度信息
			$toURL = "home.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "main&login", $_SESSION["userInfo"]["user_id"]);
			$this->display($toURL);
		}
	}
	
	/**
	 * 退出系统
	 */
	public function logout(){
		// 记录并调度信息
		$toURL = "index.htm";
		$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
		$dispatchBiz->recordDispatch($toURL, "main&logout", $_SESSION["userInfo"]["user_id"]);
		// 清空session
		$_SESSION = array();
		session_destroy();
		if (isset($_COOKIE[session_name()])){
			setcookie(session_name(), '', time()-42000, '/');
		}
		// 调度到相应页面
		$this->display($toURL);
	}
}
?>
